package com.miracle9.game.service;

import com.miracle9.game.dao.AdminServiceDao;
import com.miracle9.game.dao.PromoterServiceDao1;
import com.miracle9.game.dao.UserServiceDao;
import com.miracle9.game.dao.LoginRecordDao;
import com.miracle9.game.bean.Pager;
import com.miracle9.game.entity.Admin;
import com.miracle9.game.entity.LoginRecord;
import com.miracle9.game.entity.Promoter;
import com.miracle9.game.entity.User;
import com.miracle9.game.util.MyUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("loginRecordService")
public class LoginRecordService
        extends BaseService {

    private static Logger logger = Logger.getLogger(UserService.class);
    @Autowired
    private LoginRecordDao loginRecordDao;
    @Autowired
    private UserServiceDao userServiceDao;
    @Autowired
    private PromoterServiceDao1 promoterServiceDao1;
    @Autowired
    private AdminServiceDao adminServiceDao;
    public static final int ADMIN = 0;
    public static final int PROMOTER = 1;
    public static final int USER = 2;

    public Map<String, Object> getUserLoginRec(int userId, String startDate, String endDate, Pager pager, IoSession session) {
        User user = (User) this.userServiceDao.getById(Integer.valueOf(userId));
        if (user == null) {
            return null;
        }
        return getLoginRec(userId, 2, user.getUsername(), startDate, endDate, pager);
    }

    public Map<String, Object> getAdminLoginRec(int adminId, String startDate, String endDate, Pager pager, IoSession session) {
        Admin admin = (Admin) this.adminServiceDao.getById(Integer.valueOf(adminId));
        return getLoginRec(adminId, 0, admin.getUsername(), startDate, endDate, pager);
    }

    public Map<String, Object> getPromoterLoginRec(int promoterId, String startDate, String endDate, Pager pager, IoSession session) {
        Promoter promoter = (Promoter) this.promoterServiceDao1.getById(Integer.valueOf(promoterId));
        if (promoter == null) {
            return null;
        }
        return getLoginRec(promoterId, 1, promoter.getUsername(), startDate, endDate, pager);
    }

    public Map<String, Object> getLoginRec(int userId, int userType, String username, String startDate, String endDate, Pager pager) {
        Map map = new HashMap();

        endDate = MyUtil.DateByDayCalendar(endDate, 1);
        if ((pager.getOrder() == "") && (pager.getOrderBy() == "")) {
            pager.setOrderBy("loginDate");
            pager.setOrder("desc");
        }
        pager = this.loginRecordDao.q_CreateSqlsPager("from LoginRecord where userId=? and loginDate>=? and loginDate<? and userType=?", pager, new Object[]{Integer.valueOf(userId), startDate, endDate, Integer.valueOf(userType)});
        List datas = new ArrayList();
        for (Object o : pager.getList()) {
            LoginRecord record = (LoginRecord) o;
            Map data = new HashMap();
            data.put("loginDate", record.getLoginDate());
            data.put("loginIp", record.getLoginIp());
            data.put("userId", Integer.valueOf(userId));
            data.put("username", username);
            data.put("userType", Integer.valueOf(userType));
            datas.add(data);
        }
        pager.setList(datas.toArray());
        map.put("pager", pager);

        return map;
    }

}
